//
// Description: 141. 环形链表
// Created by Loading on 2022/7/17.
//

#include <bits/stdc++.h>
#include <Data_Structure_Definition.h>

using namespace std;

bool hasCycle(ListNode *head) {
    if (!head || !head->next) {
        return false;
    }
    // 快慢指针
    auto fast = head;
    auto slow = head;
    do {
        slow = slow->next;
        fast = fast->next->next;
        if (!fast || !fast->next) {
            return false;
        }
    } while (slow != fast);

    return true;
}

int main() {
    auto head = new ListNode(3);
    auto circle = head->next = new ListNode(2);
    head->next->next = new ListNode(0);
    head->next->next->next = new ListNode(-4);
    head->next->next->next->next = circle;

    auto res = hasCycle(head);

    cout << res << endl;

    return 0;
}